package com.itheima.mapper;

import com.itheima.pojo.Flux;
import com.itheima.pojo.Meteorology;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MeteorologyMapper {

    // 插入数据
    @Insert("insert into meteorology (timestamp, taAvg, drAvg, parAvg, rainTot, vmc5cmAvg, tem5cmAvg, waterLevel) " +
            "values (#{timestamp}, #{taAvg}, #{drAvg}, #{parAvg}, #{rainTot}, #{vmc5cmAvg}, #{tem5cmAvg}, #{waterLevel})")
    void add(Meteorology meteorology);

    // 查询数据，根据TimeStamp
    @Select("select * from meteorology where timestamp = #{timestamp}")
    Meteorology findMeteorologyByTimestamp(LocalDateTime timestamp);

    // 查询数据，返回在TimeStamp在startTime到EndTime之间的所有数据
    @Select("select * from meteorology where timestamp between #{startTime} and #{endTime}")
    List<Meteorology> findMeteorologyByTimeRange(LocalDateTime startTime, LocalDateTime endTime);

    // 更新数据
    @Update("update meteorology set taAvg = #{taAvg}, drAvg = #{drAvg}, parAvg = #{parAvg}, rainTot = #{rainTot}, vmc5cmAvg = #{vmc5cmAvg}, tem5cmAvg = #{tem5cmAvg}, waterLevel = #{waterLevel} where timestamp = #{timestamp}")
    void update(Meteorology meteorology);

    // 查询默认时间戳最新的数据
    @Select("SELECT * FROM meteorology ORDER BY timestamp DESC LIMIT 1")
    Meteorology findLatestMeteorology();

    // 查询数据库中现有数据的最新日期的当天的全部数据
    @Select("select * from meteorology where date(timestamp) = (select max(date(timestamp)) from meteorology)")
    List<Meteorology> findMeteorologyInLatestDayData();

    // 查询表中的数据总条数
    @Select("select count(*) from meteorology")
    Integer findMeteorologyAllNumber();
}
